package com.xywh.emergency_outbound.mvp.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xywh.emergency_outbound.R;
import com.xywh.emergency_outbound.mvp.ui.entity.Tab1;

import java.util.List;


public class Tab1Adapter extends BaseQuickAdapter<Tab1.DataBean, BaseViewHolder> {

    private Context mContext;
    private IMaterialLocalData mIMaterialLocalData;

    public Tab1Adapter(Context context, @Nullable List<Tab1.DataBean> data, IMaterialLocalData iMaterialLocalData) {
        super(R.layout.item_tab1, data);
        this.mContext = context;
        this.mIMaterialLocalData = iMaterialLocalData;
    }

    @Override
    protected void convert(BaseViewHolder helper, Tab1.DataBean item) {
        //GlideUtil.loadImage(mContext,item.getImg(),helper.getView(R.id.img_head));
        helper.setText(R.id.tv_material, "物资：" + item.getMaterialName());
        helper.setText(R.id.tv_warehouse, "仓库：" + item.getStoreName());
        helper.setText(R.id.tv_quantity, "数量：" + item.getStoreNum());
        CheckBox checkBox = helper.getView(R.id.cb);
        if (checkBox != null) {
            checkBox.setOnCheckedChangeListener(null);
            if (item.getIsCheck()) {
                checkBox.setChecked(true);
            } else {
                checkBox.setChecked(false);
            }
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    if (isChecked) {
                        mIMaterialLocalData.insertMaterial(item);
                    } else {
                        mIMaterialLocalData.delMaterial(item);
                    }
                }
            });
        }

        ImageView img_add = helper.getView(R.id.img_add);
        ImageView img_minus = helper.getView(R.id.img_minus);

        img_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img_add.setVisibility(View.GONE);
                img_minus.setVisibility(View.VISIBLE);
                mIMaterialLocalData.insertMaterial(item);
            }
        });
        img_minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                img_add.setVisibility(View.VISIBLE);
                img_minus.setVisibility(View.GONE);
                mIMaterialLocalData.delMaterial(item);
            }
        });
    }

    public interface IMaterialLocalData {

        void insertMaterial(Tab1.DataBean dataBean);

        void delMaterial(Tab1.DataBean dataBean);

    }

}
